package net.seehope.foodie.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import net.seehope.foodie.mapper.ItemsCommentsMapper;
import net.seehope.foodie.mapper.ItemsImgMapper;
import net.seehope.foodie.mapper.ItemsMapper;
import net.seehope.foodie.mapper.ItemsParamMapper;
import net.seehope.foodie.mapper.ItemsSpecMapper;
import net.seehope.foodie.pojo.Items;
import net.seehope.foodie.pojo.ItemsComments;
import net.seehope.foodie.pojo.ItemsImg;
import net.seehope.foodie.pojo.ItemsParam;
import net.seehope.foodie.pojo.ItemsSpec;
import net.seehope.foodie.pojo.bo.CommentsBo;
import net.seehope.foodie.pojo.bo.SearchBo;
import net.seehope.foodie.pojo.bo.SearchByCatsBo;
import net.seehope.foodie.pojo.vo.CommentRecordVo;
import net.seehope.foodie.pojo.vo.CountsVo;
import net.seehope.foodie.pojo.vo.ItemInfoVo;
import net.seehope.foodie.pojo.vo.PageGridResultVo;
import net.seehope.foodie.pojo.vo.ShopCartItemVo;
import net.seehope.foodie.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService{
	
	@Autowired
	private ItemsMapper itemsMapper;
	
	@Autowired
	private ItemsImgMapper itemsImgMapper;
	
	@Autowired
	private ItemsParamMapper itemsParamMapper;
	
	@Autowired
	private ItemsSpecMapper itemsSpecMapper;
	
	@Autowired
	private ItemsCommentsMapper itemsCommentsMapper;
	
	
	@Override
	public PageGridResultVo renderSearchItem(SearchBo bo) {
		
		String sort = bo.getSort();
		if(StringUtils.equals(sort,"k")||StringUtils.equals(sort,"c")||StringUtils.equals(sort,"p")) {
			
			//实现分页
			PageHelper.startPage(bo.getPage(),bo.getPageSize());
			//返回给前端的vo对象
			PageGridResultVo pageGridResultVo = new PageGridResultVo();
			pageGridResultVo.setRows(itemsMapper.queryItemVoLikeName(bo));
			
			pageGridResultVo.setPage(bo.getPage());
			
			pageGridResultVo.setRecords(itemsMapper.queryItemVoRecords(bo));
			
			Long records = itemsMapper.queryItemVoRecords(bo);
			
			double doubleValue = new Long(records).doubleValue();
			Integer pageSize = bo.getPageSize();
			
			pageGridResultVo.setTotal((int)Math.ceil(doubleValue/pageSize));
			
			return pageGridResultVo;
			
			
		}else {
			throw new RuntimeException("错误!");
		}
		
	}


	@Override
	public PageGridResultVo renderSearchItem(SearchByCatsBo bo) {
		String sort = bo.getSort();
		if(StringUtils.equals(sort,"k")||StringUtils.equals(sort,"c")||StringUtils.equals(sort,"p")) {
			PageHelper.startPage(bo.getPage(),bo.getPageSize());
			PageGridResultVo pageGridResultVo = new PageGridResultVo();
			pageGridResultVo.setRows(itemsMapper.queryItemVoByCats(bo));
			pageGridResultVo.setPage(bo.getPage());
			return pageGridResultVo;
						
		}else {
			throw new RuntimeException("错误!");
		}
		
	}


	@Override
	public ItemInfoVo renderItemInfo(String itemId) {
		//查四张表
		ItemInfoVo itemInfoVo = new ItemInfoVo();
		Items items = new Items();
		items.setId(itemId);
		
		ItemsImg itemsImg = new ItemsImg();
		itemsImg.setItemId(itemId);
		
		ItemsParam itemsParam = new ItemsParam();
		itemsParam.setItemId(itemId);
		
		ItemsSpec itemsSpec = new ItemsSpec();
		itemsSpec.setItemId(itemId);
		
		itemInfoVo.setItem(itemsMapper.selectOne(items));
		itemInfoVo.setItemParams(itemsParamMapper.selectOne(itemsParam));
		itemInfoVo.setItemImgList(itemsImgMapper.select(itemsImg));
		itemInfoVo.setItemSpecList(itemsSpecMapper.select(itemsSpec));
		
		return itemInfoVo;
	}

	//查询评论数目
	@Override
	public CountsVo renderCommentLevelCounts(String itemId) {
		CountsVo countsVo = new CountsVo();
		
		ItemsComments itemsComments1 = new ItemsComments();
		itemsComments1.setItemId(itemId);
		countsVo.setTotalCounts(itemsCommentsMapper.selectCount(itemsComments1));
		
		ItemsComments itemsComments2 = new ItemsComments();
		itemsComments2.setItemId(itemId);
		itemsComments2.setCommentLevel(1);
		countsVo.setGoodCounts(itemsCommentsMapper.selectCount(itemsComments2));
		
		ItemsComments itemsComments3 = new ItemsComments();
		itemsComments3.setItemId(itemId);
		itemsComments3.setCommentLevel(2);
		countsVo.setNormalCounts(itemsCommentsMapper.selectCount(itemsComments3));
		
		ItemsComments itemsComments4 = new ItemsComments();
		itemsComments4.setItemId(itemId);
		itemsComments4.setCommentLevel(3);
		countsVo.setBadCounts(itemsCommentsMapper.selectCount(itemsComments4));
		
		return countsVo;
	}


	@Override
	public PageGridResultVo renderCommentsByLevel(CommentsBo bo) {
		//分页
		PageHelper.startPage(bo.getPage(),bo.getPageSize());
		PageGridResultVo pageGridResultVo = new PageGridResultVo();
		
		List<CommentRecordVo> renderCommentByItemlevel = itemsCommentsMapper.renderCommentByItemlevel(bo);
		
		
		
		pageGridResultVo.setRows(renderCommentByItemlevel);
		Integer i = itemsCommentsMapper.selectCommentRecords(bo);
		String string = i.toString();
		pageGridResultVo.setRecords(Long.parseLong(string));
		pageGridResultVo.setPage(bo.getPage());
		double a = (double)i/bo.getPageSize();
		pageGridResultVo.setTotal((int)Math.ceil(a));
		
		return pageGridResultVo;
	}


	@Override
	public List<ShopCartItemVo> renderShopCart(String itemSpecIds) {
		String[] strings = StringUtils.splitByWholeSeparatorPreserveAllTokens(itemSpecIds, ",");
		return itemsMapper.queryShopCartItemBySpecIds(strings);
	}


	@Transactional
	@Override
	public int decreaseItemSpecsStock(String itemsSpecId, int decreaseCount) {
		
		ItemsSpec itemsSpec = new ItemsSpec();
		itemsSpec.setStock(decreaseCount);
		itemsSpec.setId(itemsSpecId);
		return itemsSpecMapper.decreaseStock(itemsSpec);
		
	}
	
	
	
	

}
